package com.campus.common.handler;

import com.campus.common.dto.ResponseDto;
import com.campus.common.exception.SystemException;
import com.campus.common.exception.ValidatorException;
import com.campus.common.exception.VerifyCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ValidatorException.class)
    @ResponseBody
    public ResponseDto validatorExceptionHandler(ValidatorException e) {
        ResponseDto responseDto = new ResponseDto<>();
        log.warn(e.getMessage());
        responseDto.setSuccess(false);
        responseDto.setMessage("请求参数异常");
        return responseDto;
    }
    @ExceptionHandler(value = SystemException.class)
    @ResponseBody
    public ResponseDto systemExceptionHandler(SystemException e) {
        ResponseDto responseDto = new ResponseDto<>();
        log.error("系统异常: {}", e.getCode().getDesc());
        responseDto.setSuccess(false);
        responseDto.setMessage(e.getCode().getDesc());
        return responseDto;
    }
    @ExceptionHandler(value = VerifyCodeException.class)
    @ResponseBody
    public ResponseDto verifyCodeExceptionHandler(VerifyCodeException e) {
        ResponseDto responseDto = new ResponseDto<>();
        responseDto.setSuccess(false);
        log.error("验证码异常: {}", e.getVerifyCodeExceptionCode().getCode());
        responseDto.setMessage(e.getVerifyCodeExceptionCode().getDesc());
        return responseDto;
    }
}
